<?php

require_once ROOT_SERVICES . DS . 'shipping_provider_interface.php';
require_once ROOT_LIBRARY_HTMLDOM . DS . 'simple_html_dom.php';

// viettel_post
class shipping_provider_10 implements shipping_provider_interface {

    private $_shipment;

    public function __construct($shipment) {
        $this->_shipment = $shipment;
    }

    public function get_status_shipping() {
        try
        {
            $tracking_number = $this->_shipment ['tracking_number'];
            $str_link = $this->_shipment ['url'] !== '' ? $this->_shipment ['url'] : "http://www.aramex.com/express/track_results_multiple.aspx?ShipmentNumber=";
            $str_link .= $tracking_number;

            $html = file_get_html($str_link);

            $status_list = array();
            list($j, $m, $Y, $H, $i, $p ) = array(0, 0, 0, 0, 0, 'AM');

            foreach ($html->find('div[id=ctl00_ctl00_MainContent_InnerMainContent_pageContent]') as $div) {
                $div_e = $div->find('div[class=spaceUp10 fullWidth leftFloat greyBorder block]', 1);
                if (is_object($div_e)) {
                    $div_c_e = $div_e->find('div[class=fullWidth leftFloat]', 0);
                    if (is_object($div_c_e)) {
                        foreach ($div_c_e->find('div.fullWidth') as $div) {
                            $arr_div = $div->find('div');
                            array_shift($arr_div);
                            $time = array_shift($arr_div);
                            $status = array_shift($arr_div);

                            $check_format_date = preg_match("/(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2}) (AM|PM)/", trim($time->plaintext), $matches);
                            $check_format_time = false;

                            if ($check_format_date) {
                                list( $date_m, $m, $j, $Y, $H, $i, $p ) = $matches;
                            } else {
                                $check_format_time = preg_match("/(\d{2}):(\d{2}) (AM|PM)/", trim($time->plaintext), $matches);
                                if ($check_format_time) {
                                    list( $time_m, $H, $i, $p) = $matches;
                                }
                            }
                            if (isset($status) && ($check_format_date || $check_format_time)) {
                                $status_list [] = array(
                                    'time' => "{$Y}-{$m}-{$j} " . date("H:i:00", strtotime("{$H}:{$i} {$p}")),
                                    'status' => "({$this->_shipment ['shipment_provider_name']}) " . $status->plaintext
                                );
                            }
                        }
                    }
                }
            }
            return array('sortable' => TRUE, 'status_list' => $status_list);
        } catch (Exception $e)
        {
            return array('sortable' => FALSE, 'status_list' => null);
        }
    }

}

?>
